﻿namespace DeltaGroup.WheelOfJeopardy.UI.Controls
{
    using System;
    using System.Windows;
    using System.Windows.Controls;

    using DeltaGroup.WheelOfJeopardy.UI.ViewModel;

    /// <summary>
    /// Interaction logic for WheelSector.xaml
    /// </summary>
    public partial class WheelSector : UserControl
    {
        #region Fields

        /// <summary>
        /// The dependency property that gets or sets the SectorInfo of the user control.
        /// </summary>
        public static DependencyProperty SectorProperty = DependencyProperty.Register("SectorInfo", typeof(SectorInfo), typeof(WheelSector));

        private readonly double STANDARD_HEIGHT = 400;
        private readonly double STANDARD_WIDTH = 400;

        #endregion Fields

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="WheelSector"/> class.
        /// </summary>
        public WheelSector()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="WheelSector"/> class.
        /// </summary>
        /// <param name="sector">The sector.</param>
        /// <param name="startAngle">The start angle.</param>
        /// <param name="endAngle">The end angle.</param>
        public WheelSector(SectorInfo sector)
            : this()
        {
            Sector = sector;
            Height = STANDARD_HEIGHT * sector.Scale;
            Width = STANDARD_WIDTH * sector.Scale;
            SetSectorStyle(sector);
        }

        #endregion Constructors

        #region Delegates

        /// <summary>
        /// Backing for WheelSectorClicked
        /// </summary>
        public delegate void WheelSectorClickedEventHandler(object sender, EventArgs e);

        #endregion Delegates

        #region Events

        /// <summary>
        /// Occurs when [wheel sector clicked].
        /// </summary>
        public event WheelSectorClickedEventHandler WheelSectorClicked;

        #endregion Events

        #region Properties

        /// <summary>
        /// Gets or sets the sector.
        /// </summary>
        /// <value>The sector.</value>
        public SectorInfo Sector
        {
            get { return (SectorInfo)GetValue(SectorProperty); }
            set { SetValue(SectorProperty, value); }
        }

        #endregion Properties

        #region Methods

        /// <summary>
        /// Handles the MouseDown event of the captionLabel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs"/> instance containing the event data.</param>
        private void captionLabel_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            RaiseWheelSectorClicked(e);
        }

        /// <summary>
        /// Raises the wheel sector clicked.
        /// </summary>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void RaiseWheelSectorClicked(EventArgs e)
        {
            if (WheelSectorClicked != null)
            {
                WheelSectorClicked(this, e);
            }
        }

        /// <summary>
        /// Sets the sector style.
        /// </summary>
        /// <param name="sector">The sector.</param>
        private void SetSectorStyle(SectorInfo sector)
        {
            try
            {
                ResourceDictionary dictionary = new ResourceDictionary();
                dictionary.Source = new Uri(sector.ResourcePath, UriKind.RelativeOrAbsolute);
                Resources = dictionary;
            }
            catch {
                // use default resource dictionary if URI is invalid
            } 
        }

        #endregion Methods
    }
}